﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
using System.Windows.Forms;

namespace ShadowHelper
{
    class Setting
    {
        public static void DoSetting(String filePath, Dictionary<String, String> paramDic)
        {
            try
            {
                // Get configuration parameters from "gui-config.json"
                StreamReader sr = new StreamReader(filePath.Remove(filePath.LastIndexOf("\\") + 1) + "gui-config.json", Encoding.Default);
                StringBuilder str = new StringBuilder();
                String line = "";
                while ((line = sr.ReadLine()) != null)
                {
                    str.Append(line);
                }
                sr.Close();
                
                JObject config = JObject.Parse(str.ToString());
                JObject server = JObject.FromObject(paramDic);
                JArray serverList = (JArray)config["configs"];

                // Modify settings
                if (str.ToString().Contains(paramDic["server"].ToString()))
                {

                    for (int i = 0; i < serverList.Count; i++)
                    {
                        JObject serverConfig = serverList[i] as JObject;
                        if (serverConfig["server"].ToString().Contains(paramDic["server"].ToString()))
                        {
                            foreach (String key in paramDic.Keys)
                            {
                                serverConfig[key] = paramDic[key];
                            }
                            config["index"] = i;
                            break;
                        }
                    }
                }
                else
                {
                    int count = serverList.Count;
                    serverList.Add(server);
                    config["index"] = count;
                }

                Debug.WriteLine(config.ToString());

                // Save settings
                StreamWriter sw = new StreamWriter(filePath.Remove(filePath.LastIndexOf("\\") + 1) + "gui-config.json", false, Encoding.UTF8);
                sw.Write(config.ToString());
                sw.Flush();
                sw.Close();

                // Restart the SS/SSR software
                Process[] pro = Process.GetProcesses();
                for (int i = 0; i < pro.Length; i++)
                {
                    Debug.WriteLine(pro[i].ProcessName.ToString() + "/" + pro[i].StartInfo.FileName + "/" + pro[i].StartInfo.WorkingDirectory);
                    // Kill the processes of SS&SSR
                    bool flag = false;
                    
                    flag = pro[i].ProcessName.ToString().Contains("Shadowsocks");

                    if (flag)
                    {
                        pro[i].Kill();
                    }
                    
                }
                Process p = Process.Start(filePath);

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
